


let Texture2D = require('./texture2d');
let myTools = require('./utils/mytools');

// let _ = require('underscore');

let texCache = {
    maps:{},

    genTexture2d(name,img){
        this.maps[name] = new Texture2D(img);
        img = null;
        return this.maps[name];
    },

    getTexture2D(name){
        return this.maps[name];
    },

    removeTexture(texture){
        for(let key in this.maps){
            if(this.maps[key] === texture){
                texture.dispose();
                this.maps[key] = null;
                delete this.maps[key];
            }
        }
    },


    async_loadImg(imgPath,aliasName){
        return new Promise(function (resolve, reject) {
            let img = new Image();
            img.src = imgPath;
            img.onload = function () {
                texCache.genTexture2d(aliasName?aliasName:imgPath,img);
                resolve();
            };
            img.onerror = function (err) {
                console.log(`error:img.src :${imgPath}`);
                reject(err);
            }
        });
    },

    /**
     *
     * @param imgs {Array|Object}
     * @return {Promise<[any, any, any, any, any, any, any, any, any, any]>}
     */
    async_preloadImgs(imgs){
        if(Array.isArray(imgs)){

            if(imgs.length < 1){
                return Promise.resolve();
            }

            let lstPromise = [];
            imgs.forEach(item=>{
                if(myTools.Types.isString(item)){
                    let p = this.async_loadImg(item);
                    lstPromise.push(p);
                }else{
                    let p = this.async_loadImg(item.path,item.name);
                    lstPromise.push(p);
                }
            });
            return Promise.all(lstPromise);
        }else{
            let lstPromise = [];
            for(let key in imgs){
                let p = this.async_loadImg(imgs[key]);
                lstPromise.push(p);
            }
            return Promise.all(lstPromise);
        }
    }
};
module.exports = texCache;



//
//
// class Texture2D{
//     constructor(img) {
//         let gl = window.app.gl;
//         let texId = this._texId = gl.createTexture();
//         gl.bindTexture(gl.TEXTURE_2D,texId);
//         gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, img);
//         gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.LINEAR);
//         gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.LINEAR);
//         gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
//         gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
//         gl.bindTexture(gl.TEXTURE_2D,null);
//
//         this.width = img.width;
//         this.height = img.height;
//     }
//
//     texId(){
//         return this._texId;
//     }
//
//     useTexture(){
//         if(!this._texId){
//             throw new Error('texId is null!');
//         }
//         let gl = window.app.gl;
//         gl.activeTexture(gl.TEXTURE0);
//         gl.bindTexture(gl.TEXTURE_2D,this._texId);
//     }
//
//     dispose(){
//         if(this._texId){
//             console.log('del texture2d');
//             let gl = window.app.gl;
//             gl.deleteTexture(this._texId);
//             this._texId = null;
//         }
//     }
// }
//
// let texCache = {
//     maps:{},
//     genTexture2d(name,img){
//         this.maps[name] = new Texture2D(img);
//         img = null;
//         return this.maps[name];
//     },
//
//     getTexture2D(name){
//         return this.maps[name];
//     },
//
//     removeTexture(texture){
//         for(let key in this.maps){
//             if(this.maps[key] === texture){
//                 texture.dispose();
//                 this.maps[key] = null;
//                 delete this.maps[key];
//             }
//         }
//     }
// };
// module.exports = texCache;
